package com.bd.scm.module.jurisdiction.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.dao.mapper.jurisdiction.BdSysLoginUserMapper;
import com.bd.scm.dao.mapper.jurisdiction.BdSysRoleMapper;
import com.bd.scm.domain.dto.jurisdiction.BdSysLoginUserDto;
import com.bd.scm.domain.models.jurisdiction.BdSysLoginUser;
import com.bd.scm.module.jurisdiction.service.BdSysLoginUserService;
import com.bd.scm.module.utils.ExamConstants;
import com.bd.scm.module.utils.MD5Utils;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/** 用户登陆ServiceImpl
 * @author YH
 * @version 1.0
 * @date 2020/12/15 8:52
 */
@Service
public class BdSysLoginUserServiceImpl implements BdSysLoginUserService {

    /**
     * 用户登陆Mapper
     */
    @Autowired
    private BdSysLoginUserMapper bdSysLoginUserMapper;

    /**
     * 角色Mapper
     */
    @Autowired
    private BdSysRoleMapper bdSysRoleMapper;

    /**
     * 获取登陆人列表
     * @param page
     * @param bdSysLoginUserDto
     * @return
     */
    @Override
    public IPage<BdSysLoginUser> getUserList(Page<BdSysLoginUser> page, BdSysLoginUserDto bdSysLoginUserDto) {
        OrderItem orderItem = OrderItem.desc("a.id");
        List<OrderItem> orderItemList = new ArrayList<>();
        orderItemList.add(orderItem);
        page.setOrders(orderItemList);
        return this.bdSysLoginUserMapper.getUserList(page,bdSysLoginUserDto);
    }

    /**
     * 登陆逻辑
     * @param account
     * @param password
     * @return
     */
    @Override
    public BdSysLoginUser dologin(String account, String password) {
        BdSysLoginUser user= this.bdSysLoginUserMapper.getUserByAccount(account);
        String loginMD5 = MD5Utils.getLoginMD5(user.getSalt(), password);
        System.out.println("加密后密码为"+loginMD5);
        System.out.println("加密后密码为"+user.getPassword());
        if(user==null || !user.getPassword().equals(loginMD5) || user.getStatus().equals(ExamConstants.USER_STATUS_DELETE)){
            return null;//账号或密码错误
        }
        return user;
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @Override
    @Transactional
    public synchronized Result addUser(BdSysLoginUser user) {
        Result result = new Result();
        int userNoCount = this.bdSysLoginUserMapper.getUserNoCount(user.getAccount());
        if(userNoCount<=0){
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("用户名没对应相关工号！");
            return result;
        }
        if(this.bdSysLoginUserMapper.getUserByAccount(user.getAccount())==null ){
            this.bdSysLoginUserMapper.addUser(user);
            //获取最大ID
            int bdSysLoginUserMaxId = this.bdSysLoginUserMapper.getBdSysLoginUserMaxId();
            user.setId(bdSysLoginUserMaxId);
            //添加人员与角色关系
            this.bdSysRoleMapper.addUserRole(user);
            //根据用户名修改dt_user密码
            this.bdSysLoginUserMapper.updateUserPassword(user);
            result.setCode(200);
            result.setSuccess(true);
            return result;
        }else {
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("用户名已存在！");
            return result;
        }
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @Override
    @Transactional
    public synchronized Result updateUser(BdSysLoginUser user) {
        Result result = new Result();
        try {
            //修改人员与角色关系
            this.bdSysRoleMapper.updateUserRole(user);
            //修改登陆人信息
            this.bdSysLoginUserMapper.updateUser(user);
            //根据用户名修改dt_user密码
            this.bdSysLoginUserMapper.updateUserPassword(user);
            result.setCode(200);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setSuccess(false);
            result.setMessage("更新失败！");
        }
        return result;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result deleteUser(int ids) {
        Result result = new Result();
        //删除人员与角色关系
        this.bdSysRoleMapper.deleteUserRole(ids);
        //删除登陆数据
        this.bdSysLoginUserMapper.deleteUser(ids);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

}
